package express

import (
	"192.168.1.75/go-pkg/logx"
	"context"
	"encoding/json"
	"sale-admin/config/mysql"
	"sale-admin/internal/dao"
	"sale-admin/internal/models"

	"192.168.1.75/go-pkg/errorx"
)

type ExpressFeeSetting struct{}

// Create 模版创建
func (ExpressFeeSetting) Create(
	ctx context.Context,
	name string,                 // 模版名称
	ShippingFromProvince string, // 发货省份
	ShippingFromCity string,     // 发货城市
	pricingType int,             // 计费方式
	freeShippingProvince string, // 多个逗号分隔
	pricingConfig string,        // 计费配置
) (string, error) {
	if name == "" {
		return "", errorx.New("模版名称不能为空", -1)
	}
	if ShippingFromProvince == "" {
		return "", errorx.New("发货省份不能为空", -1)
	}
	if ShippingFromCity == "" {
		return "", errorx.New("发货城市不能为空", -1)
	}
	if pricingType == 0 {
		return "", errorx.New("计价方式不能为空", -1)
	}
	if pricingType != 1 && pricingType != 2 {
		return "", errorx.New("计价方式有误", -1)
	}

	logx.Info(ctx, "Express0801 ExpressFeeSetting.Create start", logx.Any("Name", name),
		logx.Any("ShippingFromProvince", ShippingFromProvince), logx.Any("ShippingFromCity", ShippingFromCity),
		logx.Any("pricingType", pricingType),
		logx.Any("freeShippingProvince", freeShippingProvince), logx.Any("pricingConfig", pricingConfig))
	id, err := dao.ExpressFeeSetting{}.Create(ctx, mysql.NewDB(), models.ExpressFeeSetting{
		Name:                 name,
		ShippingFromProvince: ShippingFromProvince,
		ShippingFromCity:     ShippingFromCity,
		PricingType:          pricingType,
		FreeShippingProvince: freeShippingProvince,
		PricingConfig:        pricingConfig,
	})
	if err != nil {
		return "", errorx.New("新增快递模版失败", -1)
	}

	logx.Info(ctx, "Express0801 ExpressFeeSetting.Create end success", logx.Any("Name", name),
		logx.Any("ShippingFromProvince", ShippingFromProvince), logx.Any("ShippingFromCity", ShippingFromCity),
		logx.Any("pricingType", pricingType),
		logx.Any("freeShippingProvince", freeShippingProvince), logx.Any("pricingConfig", pricingConfig))
	return id, nil
}

// Update 模版更新
func (ExpressFeeSetting) Update(
	ctx context.Context,
	ID string,
	name string,
	ShippingFromProvince string, // 发货省份
	ShippingFromCity string,     // 发货省份
	pricingType int,             // 计费方式
	freeShippingProvince string, // 多个逗号分隔
	pricingConfig string,        // 计费配置
) error {
	logx.Info(ctx, "Express0801 ExpressFeeSetting.Update start", logx.Any("ID", ID))
	success := dao.ExpressFeeSetting{}.Update(ctx, mysql.NewDB(), ID, models.ExpressFeeSetting{
		Name:                 name,
		ShippingFromProvince: ShippingFromProvince,
		ShippingFromCity:     ShippingFromCity,
		PricingType:          pricingType,
		FreeShippingProvince: freeShippingProvince,
		PricingConfig:        pricingConfig,
	})
	if !success {
		return errorx.New("更新失败", -1)
	}

	logx.Info(ctx, "Express0801 ExpressFeeSetting.Update end success", logx.Any("Name", name),
		logx.Any("ShippingFromProvince", ShippingFromProvince), logx.Any("ShippingFromCity", ShippingFromCity),
		logx.Any("pricingType", pricingType),
		logx.Any("freeShippingProvince", freeShippingProvince), logx.Any("pricingConfig", pricingConfig))
	return nil
}

// Delete 模版删除
func (ExpressFeeSetting) Delete(
	ctx context.Context,
	ID string,
) error {
	detail, _ := ExpressFeeSetting{}.Detail(ctx, ID)
	if detail == nil {
		return errorx.New("模板不存在", -1)
	}
	if detail.Default == 1 {
		return errorx.New("不能删除默认模版", -1)
	}
	logx.Info(ctx, "Express0801 ExpressFeeSetting.Delete start", logx.Any("ID", ID))
	err := dao.ExpressFeeSetting{}.Delete(ctx, mysql.NewDB(), ID)
	if err != nil {
		return errorx.New("删除失败", -1)
	}

	logx.Info(ctx, "Express0801 ExpressFeeSetting.Delete end success:", logx.Any("ID", ID))
	return nil
}

// List 模版列表
func (ExpressFeeSetting) List(
	ctx context.Context,
	name string,
	pricingType int, // 计价方式 1-计件 2-计重
	page, limit int,
) (list []dao.ExpressFeeSettingT, total int64) {
	records, total := dao.ExpressFeeSetting{}.List(ctx, mysql.NewDB(), name, pricingType, page, limit)
	for _, record := range records {
		var config []dao.PricingConfigT
		json.Unmarshal([]byte(record.PricingConfig), &config)
		// 商品统计
		var goodsCount int64
		mysql.NewDB().Model(&models.MaGoodsInfo{}).Where("express_fee_template = ?", record.ID).Count(&goodsCount)
		list = append(list, dao.ExpressFeeSettingT{
			ID:                   record.ID,
			Name:                 record.Name,
			ShippingFromProvince: record.ShippingFromProvince,
			ShippingFromCity:     record.ShippingFromCity,
			PricingType:          record.PricingType,
			FreeShippingProvince: record.FreeShippingProvince,
			Default:              record.Default,
			PricingConfig:        config,
			GoodsCount:           goodsCount,
		})
	}
	return
}

// Detail 模版详情
func (ExpressFeeSetting) Detail(
	ctx context.Context,
	ID string,
) (*dao.ExpressFeeSettingT, error) {
	var record dao.ExpressFeeSettingT
	detail := dao.ExpressFeeSetting{}.Detail(ctx, mysql.NewDB(), ID)
	if detail != nil {
		record.ID = detail.ID
		record.Name = detail.Name
		record.ShippingFromProvince = detail.ShippingFromProvince
		record.ShippingFromCity = detail.ShippingFromCity
		record.PricingType = detail.PricingType
		record.FreeShippingProvince = detail.FreeShippingProvince
		var config []dao.PricingConfigT
		json.Unmarshal([]byte(detail.PricingConfig), &config)
		record.PricingConfig = config

		logx.Info(ctx, "Express0801 ExpressFeeSetting.Detail", logx.Any("ID", ID), logx.Any("record", record))
		return &record, nil
	}

	logx.Info(ctx, "Express0801 ExpressFeeSetting.Detail", logx.Any("ID", ID), logx.Any("err", "记录不存在"))
	return nil, errorx.New("记录不存在", -1)
}
